package com.zr.task.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;

import com.zr.common.annotation.Anonymous;
import com.zr.task.domain.EimTaskLog;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.zr.common.annotation.Log;
import com.zr.common.core.controller.BaseController;
import com.zr.common.core.domain.AjaxResult;
import com.zr.common.enums.BusinessType;
import com.zr.task.domain.EimTaskTranshipment;
import com.zr.task.service.IEimTaskTranshipmentService;
import com.zr.common.utils.poi.ExcelUtil;
import com.zr.common.core.page.TableDataInfo;

/**
 * 转运任务单Controller
 * 
 * @author zhaojunqi
 * @date 2022-10-21
 */
@RestController
@RequestMapping("/eim/transhipment")
public class EimTaskTranshipmentController extends BaseController
{
    @Autowired
    private IEimTaskTranshipmentService eimTaskTranshipmentService;

    /**
     * 开始转运任务
     * @param eimTaskTranshipment 转运详情
     */
    @PostMapping("/start")
    @Anonymous
    @PermitAll
    public void start(EimTaskTranshipment eimTaskTranshipment)
    {
        //开始时间
        eimTaskTranshipment.setTranshipmentBeginTime(new Date());
        //更新转运详情表
        eimTaskTranshipmentService.updateEimTaskTranshipmentById(eimTaskTranshipment);

    }

    /**
     * 完成转运任务
     * @param eimTaskTranshipment 转运详情
     */
    @PostMapping("/end")
    @Anonymous
    @PermitAll
    @Transactional
    public void end(EimTaskTranshipment eimTaskTranshipment, @RequestParam("personId") String personId, @RequestParam("taskType") String taskType)
    {
        //结束时间
        eimTaskTranshipment.setTranshipmentEndTime(new Date());
        //更新转运详情表
        eimTaskTranshipmentService.updateEimTaskTranshipmentById(eimTaskTranshipment);
        //更新父表
        eimTaskTranshipmentService.updateEimTaskCommandSchedule(eimTaskTranshipment.getPid());
        //增加完成日志
        EimTaskLog eimTaskLog = new EimTaskLog();
        eimTaskLog.setTaskId(Long.parseLong(eimTaskTranshipment.getPid()));
        eimTaskLog.setTaskType(taskType);
        eimTaskLog.setTaskStatus("3");
        eimTaskLog.setTaskUserId(personId);
        eimTaskLog.setCreateTime(new Date());
        eimTaskTranshipmentService.addTaskTranshipmentLog(eimTaskLog);
    }

    /**
     * 查询转运任务单列表
     * @param eimTaskTranshipment 转运详情
     * @return
     */
    @GetMapping("/list")
    @Anonymous
    @PermitAll
    public TableDataInfo list(EimTaskTranshipment eimTaskTranshipment)
    {
        startPage();
        List<EimTaskTranshipment> list = eimTaskTranshipmentService.selectEimTaskTranshipmentList(eimTaskTranshipment);
        return getDataTable(list);
    }

    /**
     * 导出转运任务单列表
     */
    @PreAuthorize("@ss.hasPermi('eim:transhipment:export')")
    @Log(title = "转运任务单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EimTaskTranshipment eimTaskTranshipment)
    {
        List<EimTaskTranshipment> list = eimTaskTranshipmentService.selectEimTaskTranshipmentList(eimTaskTranshipment);
        ExcelUtil<EimTaskTranshipment> util = new ExcelUtil<EimTaskTranshipment>(EimTaskTranshipment.class);
        util.exportExcel(response, list, "转运任务单数据");
    }

    /**
     * 获取转运任务单详细信息
     */
    @GetMapping(value = "/detail")
    public AjaxResult getInfo(@RequestParam("id") Long id)
    {
        return AjaxResult.success(eimTaskTranshipmentService.selectEimTaskTranshipmentById(id));
    }

    /**
     * 新增转运任务单
     */
    @PreAuthorize("@ss.hasPermi('eim:transhipment:add')")
    @Log(title = "转运任务单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EimTaskTranshipment eimTaskTranshipment)
    {
        return toAjax(eimTaskTranshipmentService.insertEimTaskTranshipment(eimTaskTranshipment));
    }

    /**
     * 修改转运任务单
     */
    @PreAuthorize("@ss.hasPermi('eim:transhipment:edit')")
    @Log(title = "转运任务单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EimTaskTranshipment eimTaskTranshipment)
    {
        return toAjax(eimTaskTranshipmentService.updateEimTaskTranshipment(eimTaskTranshipment));
    }

    /**
     * 删除转运任务单
     */
    @PreAuthorize("@ss.hasPermi('eim:transhipment:remove')")
    @Log(title = "转运任务单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eimTaskTranshipmentService.deleteEimTaskTranshipmentByIds(ids));
    }

    /**
     * 创建子任务
     * 需求：APP
     */
    @PostMapping("/addByLeader")
    public AjaxResult addByLeader(EimTaskTranshipment eimTaskTranshipment)
    {
        eimTaskTranshipmentService.insertEimTaskTranshipment(eimTaskTranshipment);
        return AjaxResult.success(eimTaskTranshipment);
    }
}
